package com.newway.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newway.back.entity.Tournament;
import com.newway.back.mapper.TournamentMapper;
import com.newway.common.result.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/system/tournament")
public class TournamentController {

    @Resource
    TournamentMapper tournamentMapper;

    @GetMapping("/list")
    public Result getList(Tournament tournament){
        Page<Tournament> tournamentPage = new Page<>(tournament.getPageNum(), tournament.getPageSize());
        QueryWrapper<Tournament> tournamentQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(tournament.getName())){
            tournamentQueryWrapper.like("name",tournament.getName());
        }
        if(StringUtils.isNotBlank(tournament.getCategory())){
            tournamentQueryWrapper.eq("category",tournament.getCategory());
        }
        tournamentQueryWrapper.orderByDesc("create_time");
        IPage iPage = tournamentMapper.selectPage(tournamentPage, tournamentQueryWrapper);
        return Result.success(iPage);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Tournament tournament){
        tournament.setCreateTime(LocalDateTime.now());
        tournamentMapper.insert(tournament);
        return Result.success();
    }

    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable("id")Integer id){
        tournamentMapper.deleteById(id);
        return Result.success();
    }
}
